﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EasyInvoice.WPF.Common
{
    /// <summary>
    /// Interaction logic for BrowseFolders.xaml
    /// </summary>
    public partial class BrowseFiles : ModernDialog
    {
        private object dummyNode = null;
        private string _selectedPath = "";

        public BrowseFiles()
        {
            InitializeComponent();

            this.RootFolder = Environment.SpecialFolder.Desktop;
            
            this.Description = "";
            foldersTreeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(foldersTreeView_SelectedItemChanged);
            DataContext = this;
            TreeViewItem root = new TreeViewItem();
            root.Header = "This Computer";
            root.FontWeight = FontWeights.Bold;
           
            root.Expanded += new RoutedEventHandler(folder_Expanded);
            foldersTreeView.Items.Add(root);

            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                string noLabel = string.Empty;
                switch (drive.DriveType)
                {
                    case DriveType.CDRom: noLabel = "DVD Drive"; break;
                    case DriveType.Fixed: noLabel = "Local Drive"; break;
                    case DriveType.Network: noLabel = "Network Drive"; break;
                }
                    TreeViewItem item = new TreeViewItem();
                    item.Header = string.Format("{0}({1})", drive.IsReady?drive.VolumeLabel:noLabel, drive.Name.Replace("\\",""));
                    item.Tag = drive.Name;
                    item.FontWeight = FontWeights.Normal;
                    item.Items.Add(dummyNode);
                    item.Expanded += new RoutedEventHandler(folder_Expanded);
                    root.Items.Add(item);
                
            }

            root.IsExpanded = true;
            this.Buttons = new Button[] { 
               // new Button(){Name = "newButton", Margin = new Thickness(0,0,0,0),  Content = "make new folder", Padding = new Thickness(16,3,16,5)},
                this.OkButton,
                this.CancelButton
            };

            //foldersTreeView.ItemsSource = new System.IO.DirectoryInfo("C:\\").EnumerateDirectories("C:\\");// System.IO.Directory.EnumerateDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            //this.DataContext = new EasyInvoice.WPF.ViewModel.BrowseFoldersViewModel() { Description = this.Description, SelectedFolder = this.SelectedFolder };
        }

        public string Description { get; set; }
        public Environment.SpecialFolder RootFolder { get; set; }
        /// <summary>
        /// only one extension is posible for the moment .sql
        /// </summary>
        public string filterExtension { get; set; }
        public string SelectedPath
        {
            get
            {
                return _selectedPath;
            }
            set
            {
                if (_selectedPath == "")
                    foldersTreeView.SelectedValuePath = value;
                _selectedPath = value;
            }
        }

        void folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == dummyNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (string s in Directory.GetDirectories(item.Tag.ToString()))
                    {
                        if (((new DirectoryInfo(s)).Attributes & FileAttributes.Hidden) == 0)
                        {
                            TreeViewItem subitem = new TreeViewItem();
                            subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
                            subitem.Tag = s;
                            subitem.FontWeight = FontWeights.Normal;
                            subitem.Items.Add(dummyNode);
                            subitem.Expanded += new RoutedEventHandler(folder_Expanded);
                            item.Items.Add(subitem);
                        }
                    }
                    foreach (string s in Directory.GetFiles(item.Tag.ToString()))
                    {
                        DirectoryInfo file = new DirectoryInfo(s);
                        if ((file.Attributes & FileAttributes.Hidden) == 0) 
                        {
                            if ((file.Extension == (this.filterExtension ==null ?file.Extension : this.filterExtension)))
                            {
                                TreeViewItem subitem = new TreeViewItem();
                                subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
                                subitem.Tag = s;
                                subitem.FontWeight = FontWeights.Normal;
                                // subitem.Items.Add(dummyNode);
                                subitem.Expanded += new RoutedEventHandler(folder_Expanded);
                                item.Items.Add(subitem);
                            }
                        }
                    }
                }
                catch (System.IO.IOException ioex)
                {
                    ModernDialog.ShowMessage(ioex.Message, "Insert disk", MessageBoxButton.OK);
                }
                catch (Exception) { }
            }
        }

        private void foldersTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tree = (TreeView)sender;
            TreeViewItem temp = ((TreeViewItem)tree.SelectedItem);

            if (temp == null || temp.Tag == null)
                return;
            temp.IsExpanded = true;
            SelectedPath = temp.Tag.ToString();
        }
    }
}
